package study;

import java.nio.charset.StandardCharsets;

/**
 * String类也支持contains()方法
 * public boolean contains(String str)          判断该字符串中是否包含参数str指定的字符串
 *
 * public byte[] getBytes()                     将字符串转换为字节数组
 *
 * public byte[] getBytes(Charset charset)      将字符串转换为字节数组
 *                  StandardCharsets.UTF_8
 * */
public class StringDemo {
    public static void main(String[] args) {
        String str = "String";
        String str2 = "String";
        String str3 = "String";
        System.out.println(str == str2);
        System.out.println(str == str3);
        System.out.println(str2 == str);
        str += " k";
        System.out.println(str == str2);
        String str4 = new String("ffff");
        System.out.println(str4);

        System.out.println(str.length());

        str = "          hello world     ";
        str = str.trim();       // 去除两边空白字符，并将去除之后的新对象存放到str，
                                // （即trim()方法不会在原对象上操作而是生成新对象再在新对象上操作）
        System.out.println(str);
        str = "thinking in java";
        boolean start = str.startsWith("thinking");
        boolean end = str.endsWith("java");
        System.out.println(start + "\n" + end);
        System.out.println(str.charAt(9));  // 获取某个下标所对应的字符

        int index = str.indexOf("in");
        System.out.println(index);
        index = str.lastIndexOf("in");
        System.out.println(index);
        index = str.indexOf("in", 3);
        System.out.println(index);
        index = str.indexOf("ccc");
        System.out.println(index);
        System.out.println(str.substring(3, 11));   // 截取子串，含头不含尾
        System.out.println(str.substring(3));       // 从某个位置开始截取到末尾的子串
        System.out.println(str.substring(str.indexOf("in"), str.lastIndexOf("in") + 2));

        String str5 = String.valueOf(100);
        String str6 = String.valueOf(true);
        System.out.println(str5 + "  " + str6);

        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
    }
}
